package fr.upmc.aladyn.transactionables.tests.application;

/**
 * <p>Test class</p>
 * @author Alexis Deluze
 *
 */
public class Application extends Thread {
	
	private boolean changeComportement;
	
	public Application(boolean changeComportement){
		this.changeComportement = changeComportement;
	}

	public void run(){
		
		// Adultes et Enfants
		
		Enfant bart;
		Enfant lisa;
		
		Adulte homer;
		Adulte marge;
		
		if(changeComportement){
			homer  = new Adulte("Homer", 48);
			marge  = new Adulte("Marge", 43);
			bart = new Enfant("Bart", 10);
			lisa = new Enfant("Lisa", 8);
		}else{
			homer  = new Adulte("Homer", 35);
			marge  = new Adulte("Marge", 30);
			bart = new Enfant("Bart", 5);
			lisa = new Enfant("Lisa", 3);
		}
		
		bart.setParents(homer, marge);
		lisa.setParents(homer, marge);
		
		// Couple
		
		Couple simpson = new Couple(homer, marge);
		
		// Operations
		
		homer.print();
		marge.print();
		
		try {
			simpson.mariage(homer, marge);
		} catch (AlreadyMariedException e) {
			printException(e);
		}
		
		homer.print();
		marge.print();
		
		try {
			simpson.mariage(homer, marge);
		} catch (AlreadyMariedException e) {
			printException(e);
		}
		
		homer.print();
		marge.print();
		
		try {	
			homer.anniversaire();
		} catch (Exception e) {
			printException(e);
		}
		
		homer.print();
		marge.print();
		
		try {
			simpson.divorce(homer, marge);
		} catch (AlreadySingleException e) {
			printException(e);
		}
		
		homer.print();
		marge.print();
		
		try {	
			simpson.divorce(homer, marge);
		} catch (AlreadySingleException e) {
			printException(e);
		}
		
		homer.print();
		marge.print();
		
		try {	
			marge.anniversaire();
		} catch (Exception e) {
			printException(e);
		}
		
		homer.print();
		marge.print();
		
		try {
			simpson.mariage(homer, marge);
		} catch (AlreadyMariedException e) {
			printException(e);
		}
		
		homer.print();
		marge.print();
		
		try {
			simpson.mariage(homer, marge);
		} catch (AlreadyMariedException e) {
			printException(e);
		}
		
		if(changeComportement){
			homer.print();
			marge.print();
			
			try{
				simpson.naissance("Maggie");
			}catch(Exception e){
				printException(e);
			}
			
			homer.print();
			marge.print();
			
			try{
				simpson.mort(homer);
			}catch(Exception e){
				printException(e);
			}
	
			homer.print();
			marge.print();
			
			try{
				simpson.mort(homer);
			}catch(Exception e){
				printException(e);
			}
		}
		
		homer.print();
		marge.print();
	}
	
	private static void printException(Exception e){
		System.out.println("##############################################################");
		System.out.println("# Exception remontee : \n# "+e.getClass());
		System.out.println("##############################################################");
	}
}
